package dao.util;

import dao.MysqlConfiguration;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DaoTools {
    private static String[] sqls={
            "select count(*) as sum from customers",
            "select count(*) as sum from merchants",
            "select count(*) as sum from services",
            "select count(*) as sum from developers",
            "select count(*) as sum from bosses"
    };
    public static int countPersonSum(String identity){
        Connection connection= MysqlConfiguration.MysqlConnection();
        String sql=identityTosql(identity);
        System.out.println(sql);
        ResultSet rs=null;
        try {
            PreparedStatement pst= connection.prepareStatement(sql);
            rs=pst.executeQuery();
        } catch (SQLException e) {
            System.out.println("创建select count(*)语句对象失败:"+e.getMessage());
        }
        int sum=0;
        try {
            rs.next();
            sum=rs.getInt("sum");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return sum;
    }
    private static String identityTosql(String identity){
        String sql=null;
        switch (identity)
        {
            case "customers":sql=sqls[0];break;
            case "merchants":sql=sqls[1];break;
            case "services":sql=sqls[2];break;
            case "developers":sql=sqls[3];break;
            case "bosses":sql=sqls[4];break;
        }
        return sql;
    }
}
